package com.ridu.project.beshid.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by user on 2017/4/11.
 */
public class DateUtil {
    private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private final static SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat format3 = new SimpleDateFormat("yyyy/MM/dd");

    public static Date parse(String str){
        try {
            if(trim(str).matches("^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$")){
                return format.parse(str);
            } else if(trim(str).matches("^\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$")){
                return format1.parse(str);
            } else if(trim(str).matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
                return format2.parse(str);
            } else if(trim(str).matches("^\\d{4}/\\d{1,2}/\\d{1,2}$")){
                return format3.parse(str);
            }
        } catch (ParseException e) {
            return null;
        }
        return null;
    }

    public static String format(Date date){
        try{
            return format.format(date);
        } catch (Exception e){
            return null;
        }
    }

    public static String format2(Date date){
        try{
            return format2.format(date);
        } catch (Exception e){
            return null;
        }
    }

    public static String trim(String str){
        try{
            return str.trim();
        } catch (Exception e){
            return "";
        }
    }

    public static Date addDay(Date date, int day) {
        return addDay(date, 0, day);
    }

    public static Date addDay(Date date, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if(month != 0){
            cal.add(Calendar.MONTH, month);
        }
        if(day != 0){
            cal.add(Calendar.DATE, day);
        }
        return cal.getTime();
    }

    public static Date addTime(Date date, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if(min != 0){
            cal.add(Calendar.MINUTE, min);
        }
        return cal.getTime();
    }

    /**
     * @param before 之前日期
     * @param after 之后日期
     * @return
     */
    public static int getDateDiff(Date before, Date after){
        if(before == null || after == null){
            return -1;
        }
        Calendar foretime = Calendar.getInstance();
        Calendar nowCalendar = Calendar.getInstance();
        foretime.setTime(before);
        nowCalendar.setTime(after);

        return  (nowCalendar.get(Calendar.YEAR) - foretime.get(Calendar.YEAR)) * 12 +
                nowCalendar.get(Calendar.MONTH) - foretime.get(Calendar.MONTH);
    }

    /**
     * 得到指定的时间
     * @param d 基础时间
     * @param d2 参考时间
     * @param diffDay 根据考时间调整基础时间
     * @return
     */
    public static String getSpecifiedTime(Date d, Date d2, int diffDay){
        Calendar baseCalendar = Calendar.getInstance();
        baseCalendar.setTime(d);
        Calendar refCalendar = Calendar.getInstance();
        refCalendar.setTime(d2);
        if(baseCalendar.get(Calendar.DATE) >= refCalendar.get(Calendar.DATE)){
            baseCalendar.set(baseCalendar.get(Calendar.YEAR), baseCalendar.get(Calendar.MONTH), refCalendar.get(Calendar.DATE)-diffDay);
            Long time = baseCalendar.getTime().getTime();
            return time.toString().substring(0, time.toString().length()-3);
        } else {
            baseCalendar.set(baseCalendar.get(Calendar.YEAR), baseCalendar.get(Calendar.MONTH)+1, refCalendar.get(Calendar.DATE)-diffDay);
            Long time = baseCalendar.getTime().getTime();
            return time.toString().substring(0, time.toString().length()-3);
        }
    }

    /**
     * @param d 基础时间
     * @param month 需要增加的月份
     * @param diffDay 根据考时间调整基础时间
     * @return
     */
    public static String addMonth(Date d, int month, int diffDay){
        Calendar baseCalendar = Calendar.getInstance();
        baseCalendar.setTime(d);
        baseCalendar.set(baseCalendar.get(Calendar.YEAR), baseCalendar.get(Calendar.MONTH)+month, baseCalendar.get(Calendar.DATE)-diffDay);
        Long time = baseCalendar.getTime().getTime();
        return time.toString().substring(0, 10);
    }

    /**
     * 得到10位时间戳
     * @param date 时间
     * @return 时间戳
     */
    public static String getTime(String date){
        Long time = parse(date).getTime();
        return time.toString().substring(0, 10);
    }

    public static boolean compareTo(Date d, int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        if(calendar.get(Calendar.DATE) > day){
            return true;
        } else {
            return false;
        }
    }

    public static Date setDate(Date d, int day){
        return setDate(d, 0, day);
    }

    public static Date setDate(Date d, int month, int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        if(day != 0){
            calendar.set(Calendar.DATE, day);
        }
        if(month != 0){
            calendar.set(Calendar.MONTH, month);
        }
        return calendar.getTime();
    }

    public static double get10Timestamp(Date d){
        Long timestamp = d.getTime();
        return Double.parseDouble(timestamp.toString().substring(0, 10));
    }

    /**
     * 得到当天
     */
    public static int getDay(Date d){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        return calendar.get(Calendar.DATE);
    }

    /**
     * 得到格式化的日期
     */
     public static String getFormatDate(Date d, String pattern){
         SimpleDateFormat format = new SimpleDateFormat(pattern);
         return format.format(d);
     }


    /*public static void main(String[] args){
        System.out.println(new Date().getTime());
        System.out.println(parse("2015-11-23 10:06:44").getTime());
        System.out.println(getDateDiff(1448244404l, new Date().getTime()));

        Date d = new Date();
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(d);
        nowCalendar.set(nowCalendar.get(Calendar.YEAR), nowCalendar.get(Calendar.MONTH), 32);
        System.out.println(format(nowCalendar.getTime()));

        System.out.println(compareTo(parse("2017-8-29 15:40:14"), 28));
        System.out.println(format(setDate(new Date(), 0, 28)));
        System.out.println( get10Timestamp(new Date()));

        BigDecimal b = new BigDecimal(1);
        System.out.println(b.compareTo(BigDecimal.ZERO));
    }*/

}
